var app = getApp()
const order = ['demo1', 'demo2', 'demo3']
const db = wx.cloud.database()  //创建数据库实例
Page({
  data: {
    plurl: [],     //评论信息
    imageUrl: [],
    title:'',
    id: [],       //博文id
    bl: 1,          //图片比例
    avatarUrl: 'images/wx.png',
    userInfo: {},       //用户信息
    openid:'',

    Fabulous:"1",
    follow:'关注',
    focus:false,
    zan:[],
    userFollow:[]
  },
  return:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  focus:function(){
    this.setData({
      focus:true
    })
  },
  focus2:function(){
    this.setData({
      
      focus:false
    })
  },
  Fabulous:function(){
    var that=this
    var Fabulous=""
    var blog=that.data.blog
    var zan=that.data.zan
    if(that.data.Fabulous=="1"){
      Fabulous="2"
      zan.push(blog._id)
      console.log(zan)
      blog.zan=blog.zan+1
      console.log(blog.zan)
    }else{
      Fabulous="1"
      for(let i=0;i<zan.length;i++){
        if(zan[i]==blog._id){
          zan.splice(i,1)
        }
      }
      console.log(zan)
      blog.zan=blog.zan-1
      console.log(blog.zan)
    }
    
    that.setData({
      zan:zan,
      Fabulous:Fabulous,
      blog:blog
    })
    this.setFabulous(blog.zan,zan)
    

    
  },
  setFabulous:function(Bzan,zan){
    wx.cloud.callFunction({
      name: "Zan",
      data: {
        BowmanId: this.data.id,
        zan:Bzan
      }
    }).then(res => {
      console.log(res.data)
      db.collection('User').where({
        _openid:this.data.openid
      }).get({
        success: res => {
         db.collection('User').doc(res.data[0]._id).update({
          // data 传入需要局部更新的数据
          data: {
            // 表示将 done 字段置为 true
            zan: zan
          },
          success: function(res) {
            console.log(res.data)
          }
        })
        }
      })
    }).catch(err => {
      console.log(err);
    })
  },
  follow:function(e){
    console.log(e.currentTarget.id)
    var openid=this.data.blog._openid
    var follow=this.data.userFollow
    if(e.currentTarget.id=="关注"){
      console.log("已关注")
      for(let i=0;i<follow.length;i++){
        if(follow[i]==openid){
          follow.splice(openid,1)
        }
      }
      this.setFollow(follow)
      console.log(follow)
    }else{
      console.log("关注")
      follow.push(openid)
      console.log(follow)
      this.setFollow(follow)
    }
    this.setData({
      follow:e.currentTarget.id
    })
  },
  setFollow:function(follow){
    db.collection('User').where({
      _openid:this.data.openid
    }).get({
      success: res => {
       db.collection('User').doc(res.data[0]._id).update({
        // data 传入需要局部更新的数据
        data: {
          // 表示将 done 字段置为 true
          follow: follow
        },
        success: function(res) {
          console.log(res.data)
        }
      })
      }
    })
  },
  commodity:function(e){
    console.log(e.currentTarget.dataset.id)
    var that=this
    wx.navigateTo({
      url: "/pages/commodity/commodity?commodity=" + e.currentTarget.dataset.id + "",
    })
  },
  //获取用户信息
  onLoad: function (option) {
    var that=this
    wx.cloud.callFunction({
      name: "login"
    }).then(res => {
      var openid=res.result.openid
      this.setData({
        openid:openid
      })
      db.collection('User').where({
        _openid:openid
        // zan:db.command.in([option.id])
      }).get({
        success: res2 => {
          var Fabulous='1'
          console.log(res2.data)
          for(let i=0;i<res2.data[0].zan.length;i++){
            if(option.id==res2.data[0].zan[i]){
              Fabulous='2'
            }
          }
          var follow=res2.data[0].follow

          // if(res2.data.length==0){
          //   that.setData({
          //     Fabulous:'1'
          //   })
          // }else{
          //   that.setData({
          //     Fabulous:'2'
          //   })
          // }
          that.setData({
            zan:res2.data[0].zan,
            Fabulous:Fabulous,
            userFollow:res2.data[0].follow
          })
          //查询博文
          db.collection('Bowman').doc(option.id).get({
            success: res3 => {
              var follow2='关注'
              for(let i=0;i<follow.length;i++){
                if(follow[i]==res3.data._openid){
                  follow2='已关注'
                }
              }

              this.setData({
                imageUrl: res3.data.imageurl,
                id: res3.data._id,
                bl: option.wbl * 2,
                options: option,
                title:res3.data.title,
                text:res3.data.text,
                blog:res3.data,
                follow:follow2
              })
              console.log(res3.data);

            }
          })
          //查询评论
          db.collection('comment').where({
            pid: option.id
          }).get({
            success: res3 => {
              this.setData({
                plurl: res3.data
              })
              console.log(res3.data);
            }
          })      


        }
      })
     
    }).catch(err => {
      console.log(err);
    })


    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
            }
          })
        }
      }
    })
  },

  onShareAppMessage() {
    return {
      title: 'scroll-view',
      path: 'page/component/pages/scroll-view/scroll-view'
    }
  },


 
  //添加评论
  a: function () {
    let pls = this.data.scan;
    let pids = this.data.id;
    var that=this
    if(pls!=""){
      db.collection('comment').add({
        data: {
          pid: pids,
          zan: 0,
          pl: pls,
          nickName: this.data.userInfo.nickName,
          avatarUrl: this.data.userInfo.avatarUrl
        },
        success: function (res) {
          that.c();
          console.log(res.data)
        }
      })
    }

    this.setData({
      scan:''
    })
  },
  //获取input的值
  aa: function (e) {
    console.log(e.detail.value)
    this.setData({
      scan: e.detail.value
    })
  },
  //查询评论
  c: function () {
    let pids = this.data.id;
    console.log("调用成功");
    db.collection('comment').where({
      pid: pids
    }).get({
      success: res => {
        this.setData({
          plurl: res.data
        })
        console.log(res.data);
      }
    })
  }
})